package com.tmptSource.service.impl;

import com.tmpt.dto.Result;
import com.tmpt.utils.DateTool;
import com.tmpt.service.impl.DataLimitService;
import com.tmpt.utils.StringUtils;
import com.tmptSource.bean.OwExecuteRecord;
import com.tmptSource.bean.OwIssueDeclare;
import com.tmptSource.bean.OwPictureMag;
import com.tmptSource.dto.OwPictureMagDto;
import com.tmptSource.mapper.OwExecuteRecordMapper;
import com.tmptSource.mapper.OwPictureMagMapper;
import com.tmptSource.mapper.OwRegisterDataMapper;
import com.tmptSource.bean.OwRegisterData;
import com.tmptSource.dto.OwRegisterDataDto;
import com.tmptSource.dto.PageDto;
import com.tmptSource.service.intf.IOwRegisterDataService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tmptSource.utils.dozer.MyMapper;
import com.tmptSource.utils.page.MyPageUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.dozer.Mapper;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by wxy on 2016/7/28.
 */
@Service(value="owRegisterDataService")
@Transactional(propagation = Propagation.REQUIRED)
public class OwRegisterDataService implements IOwRegisterDataService {

    private Logger log = Logger.getLogger(this.getClass());
    @Autowired private DataLimitService dataLimitService;
    @Autowired private Mapper mapper;
    @Autowired private MyMapper myMapper;
    @Autowired private MyPageUtils myPageUtils;
    @Autowired
    private OwRegisterDataMapper owRegisterDataMapper;
    @Autowired
    private OwPictureMagMapper owPictureMagMapper;
    @Autowired
    private OwExecuteRecordMapper owExecuteRecordMapper;



    @Override
    public Result<OwRegisterDataDto> pagination(OwRegisterDataDto owRegisterDataDto,PageDto pageDto) {
        //分页查询
        Result<OwRegisterDataDto> result = myPageUtils.pagination(
                owRegisterDataDto,pageDto,
                OwRegisterData.class,
                OwRegisterDataDto.class,
                owRegisterDataMapper,
                false,null);

        return result;
    }

    /**
    * 分布查询，有数据边界控制功能
    * @param owRegisterDataDto
    * @param pageDto
    * @param paramMap
    * @return
    */
    @Override
    public Result<OwRegisterDataDto> pagination(OwRegisterDataDto owRegisterDataDto,PageDto pageDto,Map<String,Object> paramMap) {
        //数据边界控制参数
        Map<String,Object> paramMap1 = dataLimitService.getLimitParam(paramMap);

        //分页查询
        Result<OwRegisterDataDto> result = myPageUtils.pagination(
                owRegisterDataDto,pageDto,
                OwRegisterData.class,
                OwRegisterDataDto.class,
                owRegisterDataMapper,
                paramMap1,
                false,null);

        return result;
    }

    /**
     * 经纬度定位查找附近
     * @param owRegisterDataDto
     * mapParam.put("lng",lng);
     * mapParam.put("lat",lat);
     * @return
     */
    @Override
    public Result<OwRegisterDataDto> selectByPaginationByLatLng(OwRegisterDataDto owRegisterDataDto, PageDto pageDto) {
        Result<OwRegisterDataDto> result = new Result();

        OwRegisterData owRegisterData = mapper.map(owRegisterDataDto, OwRegisterData.class);
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page", pageDto);
        mapParam.put("bean", owRegisterData);

        //pagination
        List<OwRegisterData> list = owRegisterDataMapper.selectByPaginationByLatLng(mapParam);
        List<OwRegisterDataDto> dtoList = myMapper.map(list, OwRegisterDataDto.class);
        result.getResultMap().put("rows",dtoList);
        //查询分页总记录数
        int total = owRegisterDataMapper.selectListCount(mapParam);
        result.getResultMap().put("total", total);

        return result.success();
    }


    @Override
    public Result<OwRegisterDataDto> selectAll() {
        Result<OwRegisterDataDto> result = new Result();

        //排序
        PageDto pageDto = new PageDto();
        pageDto.setSortColumn("id");
        pageDto.setSortOrder("desc");
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("page", pageDto);

        List<OwRegisterData> list = owRegisterDataMapper.selectAll(mapParam);
        List<OwRegisterDataDto> dtoList = myMapper.map(list, OwRegisterDataDto.class);
        result.setResultList(dtoList);

        return result.success();
    }

    @Override
    public Result<OwRegisterDataDto> selectById(long id) {
        Result<OwRegisterDataDto> result = new Result();

        OwRegisterData owRegisterData = owRegisterDataMapper.selectById(id);
        OwRegisterDataDto dto = mapper.map(owRegisterData,OwRegisterDataDto.class);
        result.setResultObj(dto);

        return result.success();
    }


    /**
        返回ID值
    */
    @Override
    public Result<OwRegisterDataDto> addData(OwRegisterDataDto owRegisterDataDto){
        Result<OwRegisterDataDto> result = new Result();

        //封闭数据
        OwRegisterData owRegisterData = mapper.map(owRegisterDataDto, OwRegisterData.class);
        owRegisterData.setCreatedate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owRegisterDataMapper.insert(owRegisterData);
        if(i==0){
            return result.fault("插入失败");
        }

        //插入多张图片
        if(owRegisterDataDto!=null && owRegisterDataDto.getOwPictureMagDtoList()!=null) {
            for (OwPictureMagDto owPictureMagDto : owRegisterDataDto.getOwPictureMagDtoList()) {
                OwPictureMag owPictureMag = mapper.map(owPictureMagDto, OwPictureMag.class);
                //1:front, 2:back （默认设1）
                owPictureMag.setFrontBackSort(
                        StringUtils.isNull(owPictureMagDto.getFrontBackSort())?1:Integer.parseInt(owPictureMagDto.getFrontBackSort()));
                owPictureMag.setOwTableType("ow_register_data_id");
                owPictureMag.setOwTableId(owRegisterData.getId());
                owPictureMag.setCompanyId(0);
                owPictureMag.setCreatemanId(0);
                owPictureMag.setModifymanId(0);
                owPictureMagMapper.insert(owPictureMag);
            }
        }

        OwRegisterDataDto dto = mapper.map(owRegisterData,OwRegisterDataDto.class);
        result.setResultObj(dto);

        //加入执勤记录中
        OwExecuteRecord owExecuteRecord = new OwExecuteRecord();
        owExecuteRecord.setBusinessTableId(owRegisterData.getId());
        owExecuteRecord.setBusinessTableName("ow_register_data");
        owExecuteRecord.setCreateman(owRegisterData.getCreateman());
        owExecuteRecord.setCreatedate(owRegisterData.getCreatedate());
        owExecuteRecord.setCompanyId(owRegisterData.getCompanyId());
        owExecuteRecord.setCreatemanId(owRegisterData.getCreatemanId());
        owExecuteRecord.setModifymanId(owRegisterData.getModifymanId());
        owExecuteRecordMapper.insert(owExecuteRecord);

        return result.success();
    }



    /**
     返回ID值
     */
    @Override
    public Result<OwRegisterDataDto> addEditData(OwRegisterDataDto owRegisterDataDto){
        OwRegisterData owRegisterData = new OwRegisterData();
        owRegisterData.setRegisterDataNumber(owRegisterDataDto.getRegisterDataNumber());
        Map<String,Object> mapParam = new HashMap<>();
        mapParam.put("bean",owRegisterData);
        List<OwRegisterData> owRegisterDataList = owRegisterDataMapper.selectByPagination(mapParam);
        if(owRegisterDataList==null || owRegisterDataList.size()==0){
            //添加数据
            return addData(owRegisterDataDto);
        }else{
            //修改数据
            OwRegisterData owRegisterData1 = owRegisterDataList.get(0);
            owRegisterDataDto.setId(""+owRegisterData1.getId());
            return editData(owRegisterDataDto);
        }
    }



    /**
        返回ID值
    */
    @Override
    public Result<OwRegisterDataDto> editData(OwRegisterDataDto owRegisterDataDto){
        Result<OwRegisterDataDto> result = new Result();

        //封装数据
        OwRegisterData owRegisterData = mapper.map(owRegisterDataDto, OwRegisterData.class);
        owRegisterData.setModifydate(DateTool.getCurTimestamp());

        //调用mapper
        long i = owRegisterDataMapper.updateById(owRegisterData);
        if(i==0){
            return result.fault("编辑数据失败");
        }

        //插入多张图片
        if(owRegisterDataDto!=null && owRegisterDataDto.getOwPictureMagDtoList()!=null) {
            //有新图片时，把老图片删除了；没有新图片的话，先别删除
            if(owRegisterDataDto.getOwPictureMagDtoList().size()>0){
                List<OwPictureMag> owPictureMagList = owPictureMagMapper.selectPictureByOwRegisterDataId(owRegisterData.getId());
                Map<String,Object> delMap = new HashMap<>();
                delMap.put("modifyman","system");
                delMap.put("modifydate",DateTool.getCurTimestamp());
                for(OwPictureMag owPictureMag : owPictureMagList) {
                    delMap.put("id",owPictureMag.getId());
                    owPictureMagMapper.deleteById(delMap);
                }
            }
            for (OwPictureMagDto owPictureMagDto : owRegisterDataDto.getOwPictureMagDtoList()) {
                OwPictureMag owPictureMag = mapper.map(owPictureMagDto, OwPictureMag.class);
                //1:front, 2:back （默认设1）
                owPictureMag.setFrontBackSort(
                        StringUtils.isNull(owPictureMagDto.getFrontBackSort())?1:Integer.parseInt(owPictureMagDto.getFrontBackSort()));
                owPictureMag.setOwTableType("ow_register_data_id");
                owPictureMag.setOwTableId(owRegisterData.getId());
                owPictureMag.setCompanyId(0);
                owPictureMag.setCreatemanId(0);
                owPictureMag.setModifymanId(0);
                owPictureMagMapper.insert(owPictureMag);
            }
        }


        OwRegisterDataDto dto = mapper.map(owRegisterData, OwRegisterDataDto.class);
        result.setResultObj(dto);

        return result.success();
    }

    /**
    伪删除记录，返回ID值
    */
    @Override
    public Result deleteById(long id,String adminMan){
        Result result = new Result();

        Map<String,Object> mapParameter = new HashMap();
        mapParameter.put("id", id);
        mapParameter.put("modifyman",adminMan);
        mapParameter.put("modifydate",DateTool.getCurTimestamp());

        //调用mapper
        long i = owRegisterDataMapper.deleteById(mapParameter);
        if(i==0){
            return result.fault("删除数据失败");
        }

        return result.success();
    }




    /**
     *页面上查询列的值，动态加载，sql语句读数据库配置而来
     */
    public java.util.List select4a2userId(){
        return owRegisterDataMapper.select4a2userId();
    }



}
